#include "Turtle.h"
#include "Resource.h"
#include <d3dx9.h>


/**--------------------------------------------------------
*Description : Constructor Turtle
*Method name : Turtle
*Parameters :  float x, float y
*Return value : 
--------------------------------------------------------**/
Turtle::Turtle(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y)
{
	/*turtle_left = new Sprite(ResourceMng::GetInst()->GetTexture(L"..\\Resources\\Images\\mario_left.png"), 150, 2);
	turtle_right = new Sprite(ResourceMng::GetInst()->GetTexture(L"..\\Resources\\Images\\mario_right.png"), 150, 2);

	turtle_cur = turtle_right;*/
	
	check_turn = false;

	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\mario.png"), 150, 0);
	_x = x;
	_y = y;
	_vx = (float)TURTLE_VX;
	_vy = 0;

	ResetRect();
}


/**--------------------------------------------------------
*Description : Destructor Turtle
*Method name : ~Turtle
*Parameters :   
*Return value : 
--------------------------------------------------------**/
Turtle::~Turtle(void)
{
	/*delete turtle_left;
	delete turtle_right;*/
}


/**--------------------------------------------------------
*Description : Update Turtle
*Method name : Update
*Parameters :  int time
*Return value :void
--------------------------------------------------------**/
void Turtle::Update(int _ScreenWidth, int _ScreenHeight, int time)
{
	_x += _vx * time;
	_y += _vy * time;

	ResetRect();

	if(_vx > 0)
	{
		TurnRight();
	}
	else
	{
		TurnLeft();
	}

	if(_x >= _ScreenWidth)
	{
		TurnLeft();
	}
	else if(_x <= 0)
	{
		TurnRight();
	}

	if(_vx != 0)
		//turtle_cur->Update(time);
		_sprite->Update(time);
}


/**--------------------------------------------------------
*Description : Render Turtle
*Method name : Render
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Turtle:: Render()
{
	//turtle_cur->Render((int)_x,(int)_y);
	_sprite->Render((int)_x,(int)_y);
}


/**--------------------------------------------------------
*Description : Turn left Turtle
*Method name : TurnLeft
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Turtle :: TurnLeft()
{
	check_turn = true;
	//turtle_cur = turtle_left;
	_sprite->SetFrame(6, 8);
	_vx = (float)-TURTLE_VX;
}


/**--------------------------------------------------------
*Description : Turn right Turtle
*Method name : TurnRight
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Turtle :: TurnRight()
{
	check_turn = false;
	//turtle_cur = turtle_right;
	_sprite->SetFrame(2, 4);
	_vx = (float)TURTLE_VX;
}

void Turtle::ResetRect()
{
	//dang them vao
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
}

OBJECT_TYPE Turtle::GetTypeObject()
{
	return OBJECT_ENEMY;
}